const api = require('../../api/api.js');
const util = require('../../utils/util.js');
const app = getApp();

Page({
  data: {
    sites: [],
    siteId: [],
    places: [],
    index: 0,
    imgUrl: app.globalData.imgUrl + 'userImage/'
  },
  onLoad(o) {
    let siteId = o.siteId;
    let entId = o.entId;
    this.setData({
      siteId,
      entId
    });
    this.queryPlantationAccount(siteId);
  },
  queryPlantationAccount(siteId) {
    let that = this;
    api.queryPlantationAccount({
      data: {
        siteId
      },
      success(res) {
        console.log(res.data);
        let managers = res.data.adminAccount;
        let site = res.data.site;
        that.setData({
          managers,
          site
        });
      }
    });
  },
  querySite() {
    let that = this;
    wx.getStorage({
      key: 'userId',
      success(res) {
        api.queryPlantation({
          data: {
            account: res.data
          },
          success(res) {
            console.log(res.data);
            let sites = res.data.plantation;
            let siteId = sites.map((i) => {
              return i.siteId;
            });
            sites = sites.map((i) => {
              return i.siteName;
            });
            if (siteId.length !== 0) {
              that.queryPlace(siteId[that.data.index]);
            }
            that.setData({
              sites: sites,
              siteId: siteId
            });
          }
        });
      }
    })
  },
  queryPlace(siteId) {
    let that = this;
    api.queryPlaces({
      data: {
        placeId: -1,
        siteId,
        limit: -1,
        pageNumber: 0,
        type: ''
      },
      success(res) {
        console.log(res.data);
        that.setData({
          places: res.data.places
        });
      }
    });
  },
  bindPickerChange(e) {
    this.queryPlace(this.data.siteId[e.detail.value]);
    this.setData({
      index: e.detail.value,
    });
  },
  goToAddManagerPage(e) {
    let siteId = this.data.siteId;
    let entId = this.data.entId;
    wx.navigateTo({
      url: `/pages/addmanager/addmanager?siteId=${siteId}&entId=${entId}`,
    });
  },
  goToManagerInfoPage(e) {
    let account = e.currentTarget.dataset.account;
    wx.navigateTo({
      url: `/pages/managerinfo/managerinfo?account=${account}`,
    })
  },
  showMsg(text) {
    util.showToast(text, 'none');
  }
})